﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms;
/*
 * 管理连接池，
 * 根据服务器带来的数据，调用多线程
 * 清理cookies
 */
namespace cn.loozo.BrokenArrow
{
    class BrowserManager
    { 
        // Wrapper method for use with thread pool.
        public void execute(List<Website> webList)
        {
            ClearCookies();
            int MaxCount = webList.Count;

            // One event is used for each Fibonacci object
            ManualEvent[] doneEvents = new ManualEvent[MaxCount];
            ThreadBrowser[] threadArray = new ThreadBrowser[MaxCount];
            // Configure and launch threads using ThreadPool:
            int workerThreads;
            int portThreads;

            ThreadPool.GetMaxThreads(out workerThreads, out portThreads);
            ThreadPool.SetMaxThreads(3, portThreads);


            for (int i = 0; i < MaxCount; i++)
            {
                doneEvents[i] = new ManualEvent();
                ThreadBrowser f = new ThreadBrowser(doneEvents[i], webList[i]);
                threadArray[i] = f;
                ThreadPool.QueueUserWorkItem(f.ThreadPoolCallback, i);
            }

            // Wait for all threads in pool to calculation... 
            while (true)
            {
                bool result = true;
                foreach (ManualEvent mre in doneEvents)
                {
                    result = result && mre.stat;
                }
                if (result)
                {
                    break;
                }
                Application.DoEvents();
            }
            ClearCookies();
        }

        private void ClearCookies()
        {
            DirectoryInfo dir = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Cookies));
            foreach (FileInfo info in dir.GetFiles("*.txt"))
            {
                info.Delete();
            }
            ClearIECache();
            ClearIECookie();
        }
        /// <summary>
        /// 清除IE缓存
        /// </summary>
        private void ClearIECache()
        {
            Process process = new Process();
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.Arguments = "/c " + "del /f /s /q \"%userprofile%\\Local Settings\\Temporary Internet Files\\*.*\"";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.CreateNoWindow = true;
            process.Start();
            string result = process.StandardOutput.ReadToEnd();
        }
        // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

        // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// 清除IE Cookie
        /// </summary>
        private void ClearIECookie()
        {
            Process process = new Process();
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.Arguments = "/c " + "del /f /s /q \"%userprofile%\\Cookies\\*.*\"";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.CreateNoWindow = true;
            process.Start();
            string result = process.StandardOutput.ReadToEnd();
        }
        // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

        // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// 关闭IE窗口
        /// </summary>
        private void KillIEWindows()
        {
            Process[] process = Process.GetProcesses();
            for (int i = 0; i < process.Length; i++)
            {
                if (process[i].ProcessName.ToLower() == "iexplore")
                {
                    try
                    {
                        process[i].Kill();
                        Thread.Sleep(500);
                    }
                    catch
                    {
                    }
                }
            }
        }
    }
}
